﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Gmantis.Controls.Toolbar
{
    public class ToolbarButton : Button, IToolbarControl
    {
        // Fields
        private string _state;
        public static readonly DependencyProperty LabelTitleProperty = DependencyProperty.Register("LabelTitle", typeof(string), typeof(ToolbarButton), new PropertyMetadata("", new PropertyChangedCallback(ToolbarButton.OnButtonChanged)));
        public static readonly DependencyProperty LargeImageSourceProperty = DependencyProperty.Register("LargeImageSource", typeof(ImageSource), typeof(ToolbarButton), new PropertyMetadata(null, new PropertyChangedCallback(ToolbarButton.OnButtonChanged)));
        public static readonly DependencyProperty MouseOverBrushProperty = DependencyProperty.Register("MouseOverBrush", typeof(Brush), typeof(ToolbarButton), null);
        public static readonly DependencyProperty PressedBrushProperty = DependencyProperty.Register("PressedBrush", typeof(Brush), typeof(ToolbarButton), null);
        public static readonly DependencyProperty SmallImageSourceProperty = DependencyProperty.Register("SmallImageSource", typeof(ImageSource), typeof(ToolbarButton), new PropertyMetadata(null, new PropertyChangedCallback(ToolbarButton.OnButtonChanged)));

        // Methods
        public ToolbarButton()
        {
            base.DefaultStyleKey = typeof(ToolbarButton);
        }

        void IToolbarControl.ChangeState(string state)
        {
            this._state = ButtonHelper.GetState(state, this.LargeImageSource, this.SmallImageSource, this.LabelTitle);
            base.InvalidateMeasure();
        }

        void IToolbarControl.FireClick()
        {
            this.OnClick();
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            if (!string.IsNullOrEmpty(this._state))
            {
                VisualStateManager.GoToState(this, this._state, false);
                this._state = null;
            }
            else
            {
                VisualStateManager.GoToState(this, ButtonHelper.GetDefaultState(this.LargeImageSource, this.SmallImageSource, this.LabelTitle), false);
            }
            return base.MeasureOverride(availableSize);
        }

        private static void OnButtonChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            ((FrameworkElement)obj).InvalidateMeasure();
        }

        // Properties
        public string LabelTitle
        {
            get { return (string)GetValue(LabelTitleProperty); }
            set { SetValue(LabelTitleProperty, value); }
        }

        public ImageSource LargeImageSource
        {
            get { return (ImageSource)GetValue(LargeImageSourceProperty); }
            set { SetValue(LargeImageSourceProperty, value); }
        }

        public Brush MouseOverBrush
        {
            get { return (Brush)GetValue(MouseOverBrushProperty); }
            set { SetValue(MouseOverBrushProperty, value); }
        }

        public Brush PressedBrush
        {
            get { return (Brush)GetValue(PressedBrushProperty); }
            set { SetValue(PressedBrushProperty, value); }
        }

        public ImageSource SmallImageSource
        {
            get { return (ImageSource)GetValue(SmallImageSourceProperty); }
            set { SetValue(SmallImageSourceProperty, value); }
        }
    }
}
